<?php
namespace Manage\Controller;
use Common\Controller\ManageBaseController;
use Think\Model;
use Common\Model\ArticleModel;
use Common\Model\RenwuModel;
use Common\Model\MemberModel;



class RenwuController extends ManageBaseController{
	/*
	 * 任务列表
	 */
	public function lists($ajax=null,$status=null,$page=null,$rows=null) {
		$map = array(); $status_view = array('default'=>'所有','del'=>'已删除','forbid'=>'禁用','allow'=>'正常');
		//查询条件 处理
		$now_status = $status_view['default'];
		if (isset($status) && key_exists($status, RenwuModel::$mystat)) { //指定查询状态
			$map['status'] = RenwuModel::$mystat[$status];
			$now_status = $status_view[$status];
		}else {
			$map['status'] = array('EGT',0); //默认查询状态为未删除的数据
		}
		if ($page <= 0) {
			$page = 1;
		}
		if ($rows > 0) {
			$listRows = $rows;
		}else {
			$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
		}
		
		//筛选条件
		$filter = I('param.');
		if(!empty($filter['cid'])){ //文章所属分类
			$map['cate']=$filter['cid'];
			$this->assign('cate', $map['cate']);
		}
		if ($filter['keyword']) {
			$map['title'] = array('like','%'.$filter['keyword'].'%');
			$this->assign('keyword', $filter['keyword']);
		}
		if ($filter['cate']>0) {
			$map['cate'] = $filter['cate'];
			$this->assign('cate', $filter['cate']);
		}else {
			$this->assign('cate', '0');
		}
		/******************/
		if ($ajax==='y') {
			$model = new Model('Renwu');
			$member = new MemberModel();
			$total = $model->where($map)->count(); //总记录数
			$list = $model->where($map)->order('updatetime desc')->page("$page,$listRows")->select();
			
			//list数据处理
			foreach ($list as &$v) {
				//发布人
				$v['mname'] = $member->where("id = ".$v['sid'])->getField("account");
				$v['time_type'] =date('Y-m-d H:i:s',$v['updatetime']); 
				//接单人
				if(!empty($v['jid'])){
					$v['jdname'] = $member->where("id = ".$v['jid'])->getField("account");
				}	
				//订单类型
				if($v['cate'] == 1){$v['catename'] = "垫付单";}
				if($v['cate'] == 2){$v['catename'] = "立付单";}
				if($v['cate'] == 3){$v['catename'] = "远程单";}
				if($v['cate'] == 4){$v['catename'] = "浏览单";}
				//订单状态
				if ($v['status'] == 0){	$v['zhuangtai'] = "任务取消";	}
				elseif ($v['status'] == 1){	$v['zhuangtai'] = "等待接单";	}
				elseif ($v['status'] == 2){	$v['zhuangtai'] = "客服审核";	}
				elseif ($v['status'] == 3){	$v['zhuangtai'] = "任务进行中";	}
				elseif ($v['status'] == 4){	$v['zhuangtai'] = "申请放款";	}
				elseif ($v['status'] == 5){	$v['zhuangtai'] = "任务完成";	}
				elseif ($v['status'] == 6){	$v['zhuangtai'] = "纠纷中";	}
				elseif ($v['status'] == 7){	$v['zhuangtai'] = "拒绝放单";	}
				elseif ($v['status'] == 8){	$v['zhuangtai'] = "刷手取消";	}
			}
			unset($v);		
// 			print_r($list);
			$list = array("total"=>(int)$total,"rows"=>$list);
			$this->ajaxReturn($list);
		}else {
			$this->assign('_page', $page);
			$this->assign('_rows', $listRows);
			$this->assign('now_status',$now_status); //当前页面筛选的状态
			
			// 记录当前列表页的cookie
			cookie(C('CURRENT_URL_NAME'),$_SERVER['REQUEST_URI']);
			$this->display();
		}
	}

	/**
	 * status 0,1 切换
	 */
	public function state($id) {
		$id = (int)$id;
	
		$model = new Model('Renwu');
		$info = $model->where("`id`=".$id)->field('id,status')->find();
		$newstate =  ($info['status'] > 0) ? '0' : '1';
		if (false === $model->where('`id`='.$id)->setField('status',$newstate)) {
			$this->error('更新失败,未知错误!');
		}
		$this->success('更新成功');
	}
}